using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.VisualScripting;
using UnityEngine;

[DefaultExecutionOrder(-100)]
public class JsonReader : MonoBehaviour
{
    public string jsonFilePath;
    public bool isInitialized = false;

    void Awake()
    {
        // Do initialization...
        isInitialized = true;
    }

    // Start is called before the first frame update
    void Start()
    {
        jsonFilePath = "Assets/config/nodes.json";
        // 判断文件是否存在
        if (File.Exists(jsonFilePath))
        {
            // 读取文件
            string jsonData = File.ReadAllText(jsonFilePath);
            // 解析json字符串
            NodeData nData = JsonUtility.FromJson<NodeData>(jsonData);
            // 创建一个字典,将配置存入字典中,方便后续调用
            Dictionary<int, NodeVo> nodesDic = new Dictionary<int, NodeVo>();

            // 遍历nodeData数组
            foreach (NodeVo innerObj in nData.data)
            {
                nodesDic.Add(innerObj.Id, innerObj);
            }
            Debug.Log("nodesDic");
            Debug.Log(nodesDic);
            // Debug.Log(JsonUtility.ToJson(nodesDic));
            // nodesDic 是字典不是list, 有key Value
            transform.gameObject.GetComponent<game>().nodesDic = nodesDic;
        }
    }

    // Update is called once per frame
    void Update() { }
}

[Serializable]
public class NodeVo
{
    public int Id;
    public string Title;
    public int Type;
    public List<int> ChildNode;
    public int Interact;
    public int triggerConditionType;
    public int InteractEffect;
    public int? ParentNode;
    public int EffectValue;
    public int Audio;
    public string Text;
}

[Serializable]
public class NodeData
{
    public List<NodeVo> data;
}
